package com.google.coffeeshop.gui.desktop.orderpayment.seatdetail;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.entity.OrderLineDTO;
import com.google.coffeeshop.entity.SeatDTO;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class SeatDetailPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private SeatTable seatTable;
	private SeatDTO seatDTO = null;
	private SeatDetailOverPanel pnlSeatDetailOver;
	private SeatDetailFrame frmSeatDetail;
	
	public SeatDetailPanel(SeatDetailFrame frmSeatDetail, SeatDTO seatDTO) {
		this.frmSeatDetail = frmSeatDetail;
		this.seatDTO = seatDTO;
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {10, 30, 30, 373, 30};
		gridBagLayout.rowHeights = new int[] {92, 20, 20, 20, 20, 20, 30, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		seatTable = new SeatTable();
		seatTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					editOrderLine();
				}
			}
		});
		seatTable.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyChar() == '\u007F') { // delete key
					removeSelectedOrderLines();
				}
			}
		});
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridheight = 3;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0, 0, 5, 5);
		gbc.gridwidth = 4;
		gbc.gridx = 0;
		gbc.gridy = 0;
		JScrollPane scrollPane = new JScrollPane(seatTable);
		add(scrollPane, gbc);
		
		pnlSeatDetailOver = new SeatDetailOverPanel(seatDTO);
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.gridwidth = 4;
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 3;
		add(pnlSeatDetailOver, gbc_panel);
		
		
		inputData();
	}
	
	private void removeSelectedOrderLines() {
		for (long orderLineId : seatTable.getSelectedProductIds()) {
			OrderLineDTO orderLine = OrderBusiness.findOrderLineById(orderLineId);
			if (orderLine != null) {
				OrderBusiness.returnStuff(
						orderLine.getOrder().getSeat().getId(), 
						orderLine.getOrder().getEmployee().getId(), 
						orderLine.getProduct().getId(), 
						orderLine.getQuantity());
				OrderBusiness.removeOrderLine(orderLineId);
			}
		}
		refreshGUI();
	}
	
	private void editOrderLine() {
		EditOrderLineFrame frmEditOrderLine = new EditOrderLineFrame(frmSeatDetail, getOrderLineId());
		frmEditOrderLine.setVisible(Boolean.TRUE);
	}
	
	private void addRowData(List<OrderLineDTO> orderLines) {
		seatTable.addNewRows(orderLines);
	}

	private void inputData() {
		List<OrderLineDTO> orderLines = OrderBusiness.findOrderLines(seatDTO.getId());
		pnlSeatDetailOver.setTotalAmount(getTotalAmount(orderLines));
		addRowData(orderLines);
	}
	
	private long getTotalAmount(List<OrderLineDTO> orderLines) {
		long totalAmount = 0;
		for (OrderLineDTO orderLine : orderLines) {
			totalAmount += orderLine.getQuantity() * orderLine.getPriceOfUnit();
		}
		return totalAmount;
	}
	
	public void saveSeatData() {
		saveOrderLines();
		pnlSeatDetailOver.saveSeat();
	}
	
	private void saveOrderLines() {
		List<OrderLineDTO> orderLines = seatTable.getOrderLines();
		OrderBusiness.saveOrderLines(orderLines);
	}
	
	public long getOrderLineId() {
		return seatTable.getSelectedProductId();
	}

	public void refreshGUI() {
		seatTable.removeAllRow();
		inputData();
		
	}
	
}
